<template>
	<div>
		<HomeHeader></HomeHeader>
		<HomeLeft :cur="2"></HomeLeft>
		<div id="main">
			<div id="web_con" v-if="isedit">
				<div class="web_wp">
					<div class="web_location">
						<div class="here_area">
							<strong>当前位置：</strong>用户管理&nbsp;&nbsp;»&nbsp;&nbsp;编辑
						</div>
						<div class="here_btn">
							<a href="javascript:history.back()" class="web_btn btn_add"><em
									class="iconfont icon-shdcfanhui"></em>返回</a>
						</div>
					</div>

					<div class="tabTitle">
						<ul class="clearfix">
							<li class="cur"><a href="javascript:;">基本信息</a></li>
						</ul>
						<div class="tabCont">
							<div class="cont">
								<table class="dataTable">
									<tr>
										<td class="ltd">姓名</td>
										<td class="rtd">
											<input type="text" v-model="nickname" class="input-text">
										</td>
									</tr>

									<tr>
										<td class="ltd">手机号</td>
										<td class="rtd">
											<input type="text" v-model="phone" disabled class="input-text">
										</td>
									</tr>

									<tr>
										<td class="ltd">身份证号</td>
										<td class="rtd">
											<input type="text" v-model="idcard" disabled class="input-text">
										</td>
									</tr>
									<tr>
										<td class="ltd">开户行</td>
										<td class="rtd">
											<input type="text" v-model="bankname" class="input-text">
										</td>
									</tr>
									<tr>
										<td class="ltd">银行卡账号</td>
										<td class="rtd">
											<input type="text" v-model="cardno" class="input-text">
										</td>
									</tr>
									<tr>
										<td class="ltd">持卡人</td>
										<td class="rtd">
											<input type="text" v-model="owner" class="input-text">
										</td>
									</tr>
									<tr>
										<td class="ltd">用户名</td>
										<td class="rtd">
											<input type="text" :value="phone" disabled class="input-text">
										</td>
									</tr>

									<tr>
										<td class="ltd">密码</td>
										<td class="rtd">
											<input type="text" v-model="password" class="input-text">
											<span class="text-red pl10">输入密码进行保存即可修改密码</span>
										</td>
									</tr>
								</table>
							</div>
						</div>
						<div>
							<div class="contBtn">
								<table class="dataTable">
									<tfoot>
										<tr>
											<td class="ltd"></td>
											<td class="rtd">
												<a href="javascript:;" class="web_btn btn_add" @click="SubMit()">
													<em class="iconfont icon-shdcbaocun"></em>保存
												</a>
												
												<navigator  style="display: inline-block;" :url="'/pages/member/shop?id=' + info.id"
													hover-class="none">
												
												<a href="javascript:;" v-if="info.guard=='user'" class="web_btn btn_add">
													<em class="iconfont icon-shdcbaocun"></em>转为商户
												</a>
												</navigator>
											</td>
										</tr>
									</tfoot>
								</table>
							</div>
						</div>
					</div>
				</div>

			</div>
		</div>
	</div>
</template>

<script>
	import HomeHeader from '../components/Header.vue';
	import HomeLeft from '../components/Left.vue';
	import PicUp from '../components/PicUp.vue'
	export default {
		components: {
			HomeHeader,
			HomeLeft,
			PicUp
		},
		data() {
			return {
				isedit: false,
				idcard: '', //身份证号
				phone: '', //手机号
				nickname: '', //用户名
				cardno: '',
				bankname: '',
				owner: '',
				password: '',
				gender: '',
				info: [],
				id: 0,
			}
		},
		onLoad(e) {
			let that = this;

			// that.isedit = true;
			if (e.id) {
				that.id = e.id;
				that.GetDeta(e.id);
			}
		},
		methods: {



			// 详情
			GetDeta: function(id) {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				that.common.http('GET', 'user/' + id, '', function(res) {
					uni.hideLoading();
					console.log(res.data)

					that.info = res.data;
					that.idcard = res.data.idcard;
					that.phone = res.data.phone;
					that.nickname = res.data.nickname;



					if (res.data.bank) {
						that.bankname = res.data.bank.bankname;
					}
					if (res.data.bank) {
						that.cardno = res.data.bank.cardno;
					}
					if (res.data.bank) {
						that.owner = res.data.bank.owner;
					}
					that.gender = res.data.gender;
					that.isedit = true;
				});
			},

			//保存
			SubMit: function() {
				let that = this;
				if ((that.bankname != '' && (that.cardno == '' || that.owner == '')) ||
					(that.cardno != '' && (that.bankname == '' || that.owner == '')) ||
					(that.owner != '' && (that.bankname == '' || that.cardno == ''))) {
					uni.showToast({
						icon: 'none',
						title: '银行卡信息不能只填一个',
						duration: 2000
					});
				} else {
					var _data = that.info;
					_data.nickname = that.nickname;
					if (that.info.bank) {
						_data.bank.cardno = that.cardno;
						_data.bank.owner = that.owner;
						_data.bank.bankname = that.bankname;
					} else if (that.cardno || that.owner || that.bankname) {
						_data.bank = {
							cardno: that.cardno || null,
							owner: that.owner || null,
							bankname: that.bankname || null
						};
					}

					if (that.password != '') {
						_data.password = that.password;
						_data.password_confirmation = that.password;
					};

					if (that.id > 0) {
						that.common.http('PUT', 'user/' + that.id, _data, function(res) {
							uni.hideLoading();
							uni.showToast({
								icon: 'none',
								title: '修改成功！',
								duration: 2000
							});
							setTimeout(function() {
								that.navigateBack();
							}, 1500);
						});
					} else {
						that.common.http('POST', 'addr/store', _data, function(res) {
							uni.hideLoading();
							uni.showToast({
								icon: 'none',
								title: '添加成功',
								duration: 2000
							});
							setTimeout(function() {
								that.navigateBack();
							}, 1500);
						});
					}
				}
			},

			navigateBack() {
				// #ifdef H5
				const pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack(1)
					return;
				}
				//使用vue-router返回上一级
				let a = this.$router.go(-1)
				if (a == undefined) {}
				return;
				// #endif
				uni.navigateBack();
			},

		}
	}
</script>

<style>


</style>